<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\talent\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\talent\model\talent;
use Illuminate\Support\Js;
use plugin\saiadmin\service\OpenSpoutWriter;
use support\think\Db;

/**
 * 人才列表逻辑层
 */
class talentLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new talent();
    }
        public function export($where = [])
    {
        $query = $this->search($where);
        // ->field('id,name,enterprise_id,sort,status,create_time');
        $data = $this->getAll($query);
       
        $file_name = '招聘人才数据.xlsx';
        $header = [ '姓名', '性别','联系电话','邮箱','年龄', '出生日期', '籍贯','现在居住地', 
        '学历', '毕业学校', '英语水平', '求职意向','教育背景','工作经历','自我介绍','附件','创建时间'];


        $filter = [
            // 'status' => [
            //     ['value' => 1, 'label' => '正常'],
            //     ['value' => 2, 'label' => '禁用']
            // ]
        ];
        $data_attr=[];
        foreach($data as $value){
          $res=$this->getTalentInfo($value['talent_id']);

           $data_attr[] = [
            'name'=>$value['name'],
            'gender'=>$value['gender']==1?'男':'女',
            'contact_number'=>$value['contact_number'],
            'email'=>$value['email'],
            'age'=>$value['age'],
            'birth_date'=>$value['birth_date'],
            'domicile'=>$value['domicile'],
            'current_residence'=>$value['current_residence'],
            'education_level'=>$value['education_level'],
            'graduation_school'=>$value['graduation_school'],
            'english_level'=>$value['english_level'],
            'career_intention'=>$value['career_intention'],
            'educationackgrounds'=>$res['educationackgrounds'],
            'work_experiences'=>$res['work_experiences'],
            'self_introduction'=>$res['self_introduction'], 
            'attachment_path'=>$res['attachment_path'], 
            'create_time'=>$value['create_time']
           ];
        }

        $writer = new OpenSpoutWriter($file_name);
        $writer->setWidth([20, 20, 20, 20, 20, 20,20,20,20,20,20,20,20,50,50,50,20]);
        $writer->setHeader($header);
        $writer->setData($data_attr, null, $filter);
        $file_path = $writer->returnFile();
        return response()->download($file_path, urlencode($file_name));
    }
    private function getTalentInfo($talent_id){
        $data=[
            'educationackgrounds'=>'',
            'work_experiences'=>'',
            'self_introduction'=>'',
            'attachment_path'=>'',
            
        ];
        $attachment_path='';
        $educationackgrounds='';
        $work_experiences='';
        $self_introduction='';
    
    
        $talent_info = Db::table('eb_talen_info')->where('talent_id',$talent_id)->select()->toArray();
        foreach($talent_info as $value){
            switch($value['type']){
                case 1:
                    $educationackgrounds_attr =!empty($value['content']) ? json_decode($value['content'],true) : '';
                    if($educationackgrounds_attr && is_array($educationackgrounds_attr)){
                        $educationackgrounds .= '开始时间:'.($educationackgrounds_attr['startTime']??' ')."\n".
                        '结束时间:'.($educationackgrounds_attr['endTime']??' ')."\n".
                        '毕业院校:'.($educationackgrounds_attr['school']??' ')."\n".
                        '专业:'.($educationackgrounds_attr['major']??' ')."\n".
                        '学历:'.($educationackgrounds_attr['education']??' ')."\n".
                        '介绍:'.($educationackgrounds_attr['description']??' ')."\n".
                        "\n\n";
    
                    }
    
    
                    break;
                case 2:
                    $work_experiences_attr =!empty($value['content']) ? json_decode($value['content'],true) : '';
                    if($work_experiences_attr && is_array($work_experiences_attr)){
                        $work_experiences .= '开始时间:'.($work_experiences_attr['startTime']??' ')."\n".
                        '结束时间:'.($work_experiences_attr['endTime']??' ')."\n".
                        '服务公司:'.($work_experiences_attr['companyName']??' ')."\n".
                        '担任职位:'.($work_experiences_attr['position']??' ')."\n".
                        '工作内容:'.($work_experiences_attr['workContent']??' ')."\n".
                        "\n\n";
    
                    }
                    break;
                case 3:
                    $self_introduction =!empty($value['content']) ? $value['content'] : '';
                    break;
                case 5:
                    $attachment_path_attr =!empty($value['content']) ? json_decode($value['content'],true) : '';
                    if($attachment_path_attr){
                        $attachment_path .= $attachment_path_attr."\n";
                    }
    
                    break;    
            }
        }
        $data['educationackgrounds'] =$educationackgrounds;
        $data['work_experiences'] = $work_experiences;
        $data['attachment_path'] = $attachment_path;
        $data['self_introduction'] = $self_introduction;
    
        
        return $data;
    }


}